package control;

import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.gallery.GalleryService;
import model.gallery.GalleryVO;
import model.member.MemberService;
import model.member.MemberVO;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class MemberController extends MultiActionController {
	private MemberService memberService;
	private GalleryService galleryService;
	public MemberController() {
		super();
		// TODO Auto-generated constructor stub
	}

	public MemberController(MemberService memberService,GalleryService galleryService) {
		super();
		this.memberService = memberService;
		this.galleryService = galleryService;
	}
	public ModelAndView view(HttpServletRequest request, HttpServletResponse response){
		String page = request.getParameter("page");
		String viewName = "home";
		if(!page.equals("home")){
			viewName = page+".member";
		}
		return new ModelAndView(viewName);
	}
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response){
		String id = request.getParameter("id");
		String password = request.getParameter("password");
		MemberVO vo = null;
		HttpSession session = request.getSession();
		try {
			vo = memberService.login(id, password);
			session.setAttribute("mvo", vo);
			session.setAttribute("log", "true");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			session.setAttribute("log", "fail");
		}
		if(vo==null){
			session.setAttribute("log", "fail");
		}
		return new ModelAndView("redirect:index.jsp");
	}
	public ModelAndView logout(HttpServletRequest request, HttpServletResponse response, HttpSession session){
		if(session!=null)
			session.invalidate();
		return new ModelAndView("redirect:index.jsp");
	}
	public ModelAndView register(HttpServletRequest request, HttpServletResponse response, MemberVO vo){
		try {
			memberService.register(vo);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ModelAndView("redirect:index.jsp");
	}
	public ModelAndView idCheck(HttpServletRequest request, HttpServletResponse response){
		String id = request.getParameter("id");
		boolean flag=true;
		try {
			flag = memberService.idCheck(id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ModelAndView("JsonView","result", flag);
	}
	public ModelAndView update(HttpServletRequest request, HttpServletResponse response,MemberVO vo){
		HttpSession session = request.getSession(false);
		MemberVO vo2 = (MemberVO) session.getAttribute("mvo");
		if(vo.getUploadImg()!=null){
			vo.setProImg(vo2.getId()+".png");
		}
		try {
			memberService.update(vo);
			MemberVO mvo = memberService.login(vo.getId(), vo.getPassword());
			session.setAttribute("mvo", mvo);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ModelAndView("redirect:index.jsp");
	}
	public ModelAndView leave(HttpServletRequest request, HttpServletResponse response, HttpSession session){
		MemberVO vo = (MemberVO) session.getAttribute("mvo");
		session.invalidate();
		try {
			memberService.leave(vo);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ModelAndView("redirect:index.jsp");
	}
	public ModelAndView memberMain(HttpServletRequest request, HttpServletResponse response){
		int uid = Integer.parseInt(request.getParameter("uid"));
		ArrayList<Object> list = null;
		try {
			list = galleryService.listByUser(uid, 1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ModelAndView("main.member","list",list);
	}
	public ModelAndView friendMain(HttpServletRequest request, HttpServletResponse response){
		int uid = Integer.parseInt(request.getParameter("uid"));
		ArrayList<Object> list = null;
		try {
			list = galleryService.listByUser(uid, 1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ModelAndView("main_friend.member","list",list);
	}
	// ---------------------- 관리자 추가 ---------------------------------
	public ModelAndView admin(HttpServletRequest request, HttpServletResponse response){
		return new ModelAndView("admin.member");
	}
}
